package cn.ppool.xmlmapper.builder.parse.model.entry;

import cn.ppool.xmlmapper.builder.MapperBuilder;
import cn.ppool.xmlmapper.builder.annotation.Select;
import cn.ppool.xmlmapper.builder.parse.model.base.BaseEntryBO;
import cn.ppool.xmlmapper.builder.parse.model.base.BaseFuncBO;
import cn.ppool.xmlmapper.builder.xml.XNode;
import cn.ppool.xmlmapper.builder.xml.XPathParser;
import cn.ppool.xmlmapper.utils.StringUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

import java.util.Arrays;
import java.util.List;
import java.lang.reflect.Method;
import java.util.Map;

@Log4j2
public class SelectBO extends BaseEntryBO {
    public final static String TAG_NAME = "SELECT";


    @Getter
    @Setter
    private Class<?> resultType;

    @Getter
    private boolean onlyOne;

    @Override
    public void buildEntityModel() {

    }

    @Override
    public Select getFunAnt() {
        return (Select) super.getFunAnt();
    }

    @Override
    public String complieEntity(Map<String, Object> allVar, Map<String, Object> sqlVar, StringBuilder sql) {
        BaseFuncBO lo = null;
        for (BaseFuncBO bo : getSqlList()) {
            sql.append(bo.complieEntity(allVar, sqlVar, sql, lo));
            lo = bo;
        }
        return sql.toString();
    }


    private void init(MapperBuilder configure, final XNode rootNode, XNode currentNode, final Method method, final String id) throws Exception {
        super.fun = TAG_NAME;
        Select selectAnt = method.getAnnotation(Select.class);
        if (selectAnt != null) {
            super.funAnt = selectAnt;
            if (selectAnt.sql().length > 0) {
                StringBuffer strSql = new StringBuffer()
                        .append("<select>").append(StringUtil.appendStrList(Arrays.asList(selectAnt.sql()), "\n")).append("</select>");
                super.originSql = strSql.toString();
                XPathParser pp = new XPathParser(strSql.toString(), false);
                currentNode = pp.evalNode("/select");
            }
        } else {
            super.originSql = currentNode.toString();
        }
        //
        this.onlyOne = method.getReturnType() != List.class;    // 这个很好理解了吧
        this.resultType = super.getMapperBuilder().getSelectObjectPackerService().getReturnClass(this, method);
        super.sqlList = super.buildModel(configure, rootNode, currentNode);
    }

    public SelectBO(MapperBuilder configure, final XNode rootNode, XNode currentNode, final Method method, final String id, SelectBO tt) throws Exception {
        super(configure, rootNode, currentNode, method, id);
        init(configure, rootNode, currentNode, method, id);
        super.originSql = tt.getOriginSql();
        super.sqlList = tt.getSqlList();
        super.redirectEntry = tt;
    }

    public SelectBO(MapperBuilder configure, final XNode rootNode, XNode currentNode, final Method method, final String id) throws Exception {
        super(configure, rootNode, currentNode, method, id);
        init(configure, rootNode, currentNode, method, id);
    }
}
